package com.shenda.analysis;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigDecimal;

import com.swak.utils.BigDecimalUtil;
import com.swak.utils.IOUtils;

/**
 * cvs 文件比对
 * 
 * @author lifeng
 * @date 2020年6月1日 上午11:11:19
 */
public class CvsCompare {

	public static void main(String[] args) throws IOException {
		File cvsFile1 = new File("C:\\Users\\lifeng\\Desktop\\数据比对\\20200519120142_AccGyr_01F4_01F4-Acc+Gyr.csv");
		File cvsFile2 = new File("C:\\Users\\lifeng\\Desktop\\数据比对\\SD79EE-20200519120142.dat-Acc+Gyr.csv");

		RandomAccessFile fis1 = new RandomAccessFile(cvsFile1, "r");
		RandomAccessFile fis2 = new RandomAccessFile(cvsFile2, "r");

		int lines = 0;
		while (true) {
			lines++;
			String line1 = fis1.readLine();
			String line2 = fis2.readLine();
			if (line1 != null && line2 != null && line1.equals(line2)) {
			} else if (line1 != null && line2 != null && isSame(line1, line2)) {
			} else if (line1 == null && line2 == null) {
				break;
			} else {
				System.out.println("数据不一致:" + lines);
				System.out.println(line1);
				System.out.println(line2);

			}
		}
		System.out.println("总行数：" + lines);
		IOUtils.closeQuietly(fis1);
		IOUtils.closeQuietly(fis2);
	}

	private static boolean isSame(String line1, String line2) {
		String[] l1s = line1.split(",");
		String[] l2s = line2.split(",");
		for (int i = 0; i < l1s.length; i++) {
			double d1 = BigDecimalUtil.floor(BigDecimalUtil.valueOf(l1s[i]).doubleValue(), 4);
			double d2 = BigDecimalUtil.floor(BigDecimalUtil.valueOf(l2s[i]).doubleValue(), 4);
			if (!BigDecimalUtil.valueEqual(BigDecimal.valueOf(d1), BigDecimal.valueOf(d2))) {
				if (!BigDecimalUtil
						.equalZero(BigDecimalUtil.valueOf(BigDecimalUtil.floor(BigDecimalUtil.sub(d1, d2), 3)))) {
					return false;
				}
				return true;
			}
		}
		return true;
	}
}